var ywy  = "";
$(document).ready(function(){ 
	ywy  = manager.findParamFromUrl('ywy') ? manager.findParamFromUrl('ywy'):'000000';//业务域（先写死）
	//监听是否登录
	manager.isLogin();
	//1.初始化UE编辑框：
	yjfkApp.loadBaiduUE();
	
	//2.初始化问题类别：
	yjfkApp.getSelectedZtVal('WTLX');	
	//3.监听保存：
	$(".hp-yjfk-submit").click(function(){
		var wtlxCode = $('.active').attr('data-code');
		var bt =$('#bt').val();
		$('#bt').val($.trim(bt));
		if($.trim(bt) == ''){
			alert({txt: "意见标题不能为空！",callBackFun: function callBackSuccessFun(){}});					
			return;
		}
		var param = {bt: $.trim(bt), wtnr: ue.getContent(), wtlx: wtlxCode, ywy: ywy}
		
		ue.setContent($.trim(param.wtnr));
		if($.trim(param.wtnr) == ''){
			alert({txt: "内容不能为空！",callBackFun: function callBackSuccessFun(){} });					
		}else{
			function sFn(){ yjfkApp.saveWtnr(param) }//确认
			confirm({txt: "确认提交吗？",successCallBackFun: sFn});	
			
		}
	});
	
	//4.加载历史数据
	yjfkApp.getHistoryByZh();
	
}); 

yjfkApp = {
	URL:{
		insert: function(){//保存意见反馈信息路径post
			return Constants.csUrlPath + "yjfk/insertYjfk";
		},
		selectbox: function(codeClass){//get
			return Constants.csUrlPath + "common/getCodeListByCodeClass?codeClass=" + codeClass;
		},
		history: function(){//意见反馈历史数据get
			return Constants.csUrlPath + "yjfk/getHistoryByZh?ywy=" + ywy;
		}
	},
	vilidateStr:function(str,id,errText){
		 if ($.trim(str) == ''){
    		 document.getElementById(id).innerHTML = errText;
    		 document.getElementById(id).style.display = "block";
             return false;
         } else {
        	 document.getElementById(id).innerHTML = '';
        	 document.getElementById(id).style.display = "none";
             return true;
         }
	},
	saveWtnr: function(param){//保存数据
		 $.ajax({
			type: "post",
			url: yjfkApp.URL.insert(),
			data: JSON.stringify(param),
			dataType: "json",
			contentType: "application/json",
			success: function(data) {//结果状态
				if(!data.result){//失败提示
					alert({txt: "提交失败！",callBackFun: callBackSuccessFun});					
					function callBackSuccessFun(){}
					return ;
				}
				var objs = {txt:"提交成功！",callBackFun:fn}//成功提示：
				success(objs);
				function fn() {window.location.reload()}	
			}
	    })
	},
	getSelectedZtVal: function(codeClass){//加载问题类别数据
        $.ajax({
			type: "get",
			url: yjfkApp.URL.selectbox(codeClass),				
			async: false,
			success: function(data) {//数据绑定
				$('#wtlxbox').html('')
				var str = '';
				for( i in data){
					if(i == 0){
						str += '<i class="active" data-code="'+ data[i].code +'" onclick="selectWtlx(this)">'+ data[i].codedesc +'</i>';
					}else{
						str += '<i data-code="'+ data[i].code +'" onclick="selectWtlx(this)">'+ data[i].codedesc +'</i>';
					}
				}
				$('#wtlxbox').append(str);
			}
	    })
	},
	getHistoryByZh: function(){//获取意见反馈历史数据信息
		 $.ajax({
			type: "get",
			url: yjfkApp.URL.history(),				
			async: false,
			success: function(data) {//数据绑定
				if(data.length > 0){
					for(var i=0,len=data.length; i<len; i++){
						var txdz = data[i].yhtx;
						if(txdz.indexOf(Constants.imgUrl.substr(0, Constants.imgUrl.length-1) + "null") > -1){
							data[i].yhtx = Constants.consumer + "img/u284.png";
						}
					}
				}
				var yjfk_ractive = new Ractive({
					el: '#yjHistory',
					template: "#yjfk-template",
					oncomplete: function() {
						yjfk_ractive.set("size",data.length);
						
						yjfk_ractive.set("historyList",data);
					}
				});
			}
	    })
	},
	loadBaiduUE: function(){
		ue = UE.getEditor("wtnr",{
			toolbars: [ [ 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', '|', 'forecolor', 'backcolor', '|', 'fontfamily', 'fontsize', '|',
			  			'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', 'preview', 'simpleupload', 'insertvideo', '|', ] ],
			initialFrameWidth : 600,
			initialFrameHeight: 170,
			scaleEnabled: true
		});
	}
}

//改变选中样式
function selectWtlx(obj){
	$(obj).addClass('active').siblings().removeClass('active');
}

//查看回复
function toggle(obj){
	$(obj).next(".hp-yjfk-hfnr").slideToggle();
	var txt = $(obj).find('span').text();
	$(obj).find('span').text('查看回复' == txt ? '收起回复' : '查看回复');
}
//图片加载出错
function imgerror(obj){
	obj.src='../../img/u284.png';
}

